home *** CD-ROM | disk | FTP | other *** search
/ HyperLib 1997 Winter - Disc 1 / HYPERLIB-1997-Winter-CD1.ISO.7z / HYPERLIB-1997-Winter-CD1.ISO / オンラインウェア / PRG / WASTE Object Handlers 1.2.2.sit / WASTE Object Handlers 1.2.2 / Handler Source / WE_hfs_Handler.c next >
Text File  |  1996-07-18  |  8KB  |  301 lines

  1. // File Object Handler for the WASTE Text Engine
  2. // by Michael F. Kamprath, kamprath@kagi.com
  3. // maintenance by John C. Daub, hsoi@eden.com
  4. //
  5. // v1.0, 12 March 1995
  6. //
  7. // v1.1, 1 April.  Fixed a a bug with draging hard drives to a WASTE instance
  8. //                 and a bix with insert file objects via InsertFileRefFromFSSpec()
  9. //
  10. // v1.2, 28 March 1996.    Minor touch-ups and updates. Added more precompiler directives.
  11. //                        Added compatability with WASTE 1.2a5.
  12. //
  13. // v1.2.2, 16 July 1996. Added compatability with WASTE 1.2 and CW9
  14. //                         Restructured installation routines
  15.  
  16. #ifndef __ICONS__
  17. #include <Icons.h>
  18. #endif
  19. #ifndef __DRAG__
  20. #include <Drag.h>
  21. #endif
  22. #ifndef __LOWMEM__
  23. #include <LowMem.h>
  24. #endif
  25. #ifndef _WASTE_
  26. #include "WASTE.h"
  27. #endif
  28.  
  29. #ifndef _WASTEOBJECTS_
  30.     #include "WASTE_Objects.h"
  31. #endif
  32.  
  33. #include "WE_hfs_Handler.h"
  34. #include "GetFileIcon.h"
  35. #include "SendFinderOpen.h"
  36.  
  37. // Local Functions
  38. static void        SizeHFSObject( Point *objectSize, HFSFlavor **theHFS );
  39.  
  40.  
  41. //
  42. // InstallHFSObject()
  43. //        Installs the HFS Object handler into WASTE.
  44. //
  45.  
  46. OSErr    InstallHFSObject( WEReference theWE )
  47. {
  48.     OSErr    iErr;
  49.  
  50. #ifdef __cplusplus
  51.     static WENewObjectUPP        newHFSUPP = NewWENewObjectProc(HandleNewHFS);
  52.     static WEDisposeObjectUPP     disposeHFSUPP = NewWEDisposeObjectProc(HandleDisposeHFS);
  53.     static WEDrawObjectUPP        drawHFSUPP = NewWEDrawObjectProc(HandleDrawHFS);
  54.     static WEClickObjectUPP        clickHFSUPP = NewWEClickObjectProc(HandleClickHFS);
  55. #else
  56.     static WENewObjectUPP        newHFSUPP = NULL;
  57.     static WEDisposeObjectUPP     disposeHFSUPP = NULL;
  58.     static WEDrawObjectUPP        drawHFSUPP = NULL;
  59.     static WEClickObjectUPP        clickHFSUPP = NULL;
  60.     
  61.     if ( newHFSUPP == NULL )
  62.         newHFSUPP = NewWENewObjectProc(HandleNewHFS);
  63.     if ( disposeHFSUPP == NULL )
  64.         disposeHFSUPP = NewWEDisposeObjectProc(HandleDisposeHFS);
  65.     if ( drawHFSUPP == NULL )    
  66.         drawHFSUPP = NewWEDrawObjectProc(HandleDrawHFS);
  67.     if ( clickHFSUPP == NULL )
  68.         clickHFSUPP = NewWEClickObjectProc(HandleClickHFS);
  69. #endif
  70.  
  71.     if ( newHFSUPP != NULL )
  72.         iErr = WEInstallObjectHandler(flavorTypeHFS, weNewHandler, (UniversalProcPtr)newHFSUPP, theWE);
  73.     else
  74.         iErr = weUnknownObjectTypeErr;
  75.     if (iErr) return(iErr);
  76.     
  77.     if ( disposeHFSUPP != NULL )
  78.         iErr = WEInstallObjectHandler(flavorTypeHFS, weDisposeHandler, (UniversalProcPtr)disposeHFSUPP, theWE);
  79.     else
  80.         iErr = weUnknownObjectTypeErr;
  81.     if (iErr) return(iErr);
  82.     
  83.     if ( drawHFSUPP != NULL)
  84.         iErr = WEInstallObjectHandler(flavorTypeHFS, weDrawHandler, (UniversalProcPtr)drawHFSUPP, theWE);
  85.     else
  86.         iErr = weUnknownObjectTypeErr;
  87.     if (iErr) return(iErr);
  88.     
  89.     if ( clickHFSUPP != NULL )
  90.         iErr = WEInstallObjectHandler(flavorTypeHFS, weClickHandler, (UniversalProcPtr)clickHFSUPP, theWE);
  91.     else
  92.         iErr = weUnknownObjectTypeErr;
  93.     if (iErr) return(iErr);
  94.     
  95.     return(noErr);
  96. }
  97.  
  98. //
  99. // New Object Handler for HFS Objects
  100. //
  101. pascal OSErr    HandleNewHFS(Point *defaultObjectSize,WEObjectReference objectRef)
  102. {
  103.     HFSFlavor    **theHFS = (HFSFlavor**)WEGetObjectDataHandle(objectRef);
  104.  
  105.     // First size the object as WASTE requires
  106.     SizeHFSObject( defaultObjectSize, theHFS);
  107.     
  108.     // Set the object refcon to 0.  This is for safety
  109.     // as later we use the refcon to store the icon handle
  110.     // for this object.
  111.     WESetObjectRefCon( objectRef, 0 );
  112.     
  113.     return(noErr);
  114. }
  115.  
  116. //
  117. // Dispose Object handler for HFS Objects
  118. //
  119. pascal OSErr    HandleDisposeHFS(WEObjectReference objectRef )
  120. {
  121.     Handle    theHFS = WEGetObjectDataHandle(objectRef);
  122.     Handle    iconCache = (Handle)WEGetObjectRefCon( objectRef );
  123.  
  124.     // If the object has an icon handle loaded, dispose of it.
  125.     if (iconCache != 0)
  126.     {
  127.         DisposeIconSuite( iconCache, true );
  128.     }
  129.     
  130.     // Dispos of the HFS data
  131.     DisposeHandle(theHFS);
  132.  
  133.     return(MemError());
  134. }
  135.  
  136. //
  137. // Draw Object Handler for HFS objects
  138. //
  139.  
  140. pascal OSErr    HandleDrawHFS(Rect *destRect, WEObjectReference objectRef )
  141. {
  142. HFSFlavor    **theHFS = (HFSFlavor**)WEGetObjectDataHandle(objectRef);
  143. Handle        iconCache = (Handle)WEGetObjectRefCon( objectRef );
  144. FontInfo    fInfo;
  145. GrafPtr        thePort;
  146. Rect        iconRect;
  147. OSErr        iErr;
  148. short        oldTextFont, oldTextSize, oldTextFace;
  149. short        sysTextFont = LMGetSysFontFam();
  150. short        sysTextSize = LMGetSysFontSize();
  151.  
  152.     // if the object's refcon did not contain an icon handle, load one.
  153.     if (iconCache == 0)
  154.     {
  155.         HLockHi( (Handle)theHFS );
  156.         if ( ((*theHFS)->fileCreator=='MACS')&&((*theHFS)->fileType=='fold') )
  157.             iErr = GetIconSuite(&iconCache,genericFolderIconResource,svAllAvailableData);
  158.         else if ( ((*theHFS)->fileCreator=='MACS')&&((*theHFS)->fileType=='disk') )
  159.         {
  160.             iErr = GetIconSuite(&iconCache,genericHardDiskIconResource,svAllAvailableData);
  161.         }
  162.         else
  163.         {
  164.             iErr = GetFileIcon( &(*theHFS)->fileSpec, svAllAvailableData, &iconCache );
  165.             
  166.             if (iErr)
  167.                 iErr = GetIconSuite(&iconCache,genericDocumentIconResource,svAllAvailableData);
  168.         }    
  169.         HUnlock( (Handle)theHFS );
  170.         if (iErr)
  171.             return(iErr);
  172.         WESetObjectRefCon( objectRef, (long)iconCache );
  173.     }
  174.     
  175.     // Determine the icon's rectangle and plot it
  176.     SetRect( &iconRect, (destRect->right + destRect->left)/2 - 16, destRect->top,
  177.                         (destRect->right + destRect->left)/2 + 16, destRect->top + 32 );
  178.     iErr = PlotIconSuite(&iconRect,(IconAlignmentType)(atVerticalCenter + atHorizontalCenter),
  179.                                             (IconTransformType)ttNone,    iconCache);
  180.                                             
  181.     // Draw the file's title.  First save old font info for port.
  182.     GetPort(&thePort);
  183.     oldTextFont = thePort->txFont;
  184.     oldTextSize = thePort->txSize;
  185.     oldTextFace = thePort->txFace;
  186.     
  187.     TextFont( sysTextFont );
  188.     TextSize( sysTextSize );
  189.     TextFace( 0 );
  190.     GetFontInfo(&fInfo);
  191.     
  192.     MoveTo( destRect->left + 1, destRect->bottom - fInfo.descent - fInfo.leading );
  193.     DrawString( (*theHFS)->fileSpec.name );
  194.     
  195.     TextFont( oldTextFont );
  196.     TextSize( oldTextSize );
  197.     TextFace( oldTextFace );
  198.     
  199.     return( iErr );
  200. }
  201.  
  202. //
  203. // Click Handler for HFS Objects.  
  204. //        Will send the finder an open selection apple event when object is clicked.
  205. //
  206.  
  207.  
  208. pascal Boolean    HandleClickHFS(    Point hitPt, 
  209.                                     short modifiers, 
  210.                                     long clickTime, 
  211.                                     WEObjectReference objectRef)
  212. {
  213. #pragma unused (hitPt, clickTime)
  214.  
  215. HFSFlavor    **theHFS;
  216.  
  217.     if (modifiers & 0x0001)         // look for double-clicks
  218.     {
  219.         theHFS = (HFSFlavor**)WEGetObjectDataHandle(objectRef);
  220.         
  221.         HLockHi( (Handle)theHFS );
  222.         SendFinderOpenAE(&(*theHFS)->fileSpec);
  223.         HUnlock( (Handle)theHFS );
  224.         
  225.         return(true);
  226.     }
  227.     else
  228.         return(false);
  229. }
  230.  
  231. //
  232. // SizeHFSObject()
  233. //        Used to return the display size of an HFS object
  234. //
  235. static void    SizeHFSObject( Point *objectSize, HFSFlavor **theHFS )
  236. {
  237. FontInfo    fInfo;
  238. GrafPtr        thePort;
  239. short        oldTextFont, oldTextSize, oldTextFace;
  240. short        strWidth, strHeight;
  241. short        sysTextFont = LMGetSysFontFam();
  242. short        sysTextSize = LMGetSysFontSize();
  243.  
  244.     GetPort(&thePort);
  245.     oldTextFont = thePort->txFont;
  246.     oldTextSize = thePort->txSize;
  247.     oldTextFace = thePort->txFace;
  248.     
  249.     TextFont( sysTextFont );
  250.     TextSize( sysTextSize );
  251.     TextFace( 0 );
  252.     
  253.     strWidth = StringWidth( (*theHFS)->fileSpec.name ) + 4;
  254.     GetFontInfo(&fInfo);
  255.     strHeight = fInfo.ascent + fInfo.descent + fInfo.leading;
  256.     
  257.     TextFont( oldTextFont );
  258.     TextSize( oldTextSize );
  259.     TextFace( oldTextFace );
  260.  
  261.     objectSize->h = ( strWidth > 32 ) ? strWidth : 32 ;
  262.     objectSize->v = 32 + strHeight + 1;    
  263. }
  264.  
  265. //
  266. // InsertFileRefFromFSSpec()
  267. //        Inserts a file object into a WASTE instance.
  268. //
  269.  
  270. OSErr    InsertFileRefFromFSSpec( FSSpec *theFile, WEReference theWE )
  271. {
  272. FInfo        fndrInfo;
  273. HFSFlavor    **theHFS;
  274. Point        theSize;
  275. OSErr        iErr;
  276.     
  277.     theHFS = (HFSFlavor**)NewHandleClear( sizeof(HFSFlavor) );
  278.     if (theHFS)
  279.     {
  280.         // First construct the HFSFlavor object from the FSSpec
  281.         iErr = FSpGetFInfo(theFile,&fndrInfo);
  282.         if (iErr)
  283.         {
  284.             DisposeHandle( (Handle)theHFS );
  285.             return(iErr);
  286.         }
  287.         HLock( (Handle)theHFS );
  288.         (*theHFS)->fileType = fndrInfo.fdType;
  289.         (*theHFS)->fileCreator = fndrInfo.fdCreator;
  290.         (*theHFS)->fdFlags = fndrInfo.fdFlags;
  291.         (*theHFS)->fileSpec = (*theFile);
  292.         HUnlock( (Handle)theHFS );
  293.         
  294.         SizeHFSObject( &theSize, theHFS );
  295.         
  296.         // Now insert it into the WEReference
  297.         WEInsertObject( flavorTypeHFS, (Handle)theHFS, theSize, theWE );
  298.     }
  299.     return(noErr);
  300. }
  301.